output and the connected TCP sockets to post console input.
"""
+ STATUS_NEW = 'new'
+ STATUS_CLOSED = 'closed'
+ STATUS_CONNECTED = 'connected'
+ STATUS_LISTENING = 'listening'
+
def __init__(self, factory, dom, console_port):
controller.Controller.__init__(self, factory, dom)
self.majorTypes = [ CMSG_CONSOLE ]
- self.status = "new"
+ self.status = self.STATUS_NEW
self.addr = None
self.conn = None
self.rbuf = xu.buffer()
return not (self.closed() or self.rbuf.empty())
def closed(self):
- return self.status == 'closed'
+ return self.status == self.STATUS_CLOSED
def connected(self):
- return self.status == 'connected'
+ return self.status == self.STATUS_CONNECTED
def close(self):
- try:
- self.status = "closed"
- if self.conn:
- self.conn.loseConnection()
- self.listener.stopListening()
- self.deregisterChannel()
- self.lostChannel()
- except Exception, ex:
- print 'ConsoleController>close>', ex
- raise
+ """Close the console controller.
+ """
+ self.lostChannel()
+
+ def lostChannel(self):
+ """The channel to the domain has been lost.
+ Cleanup: disconnect TCP connections and listeners, notify the controller.
+ """
+ self.status = self.STATUS_CLOSED
+ if self.conn:
+ self.conn.loseConnection()
+ self.listener.stopListening()
+ controller.Controller.lostChannel(self)
def listen(self):
"""Listen for TCP connections to the console port..
"""
if self.closed(): return
- self.status = "listening"
+ self.status = self.STATUS_LISTENING
if self.listener:
#self.listener.startListening()
pass
self.listener = reactor.listenTCP(self.console_port, f)
def connect(self, addr, conn):
+ """Connect a TCP connection to the console.
+ Fails if closed or already connected.
+
+ addr peer address
+ conn connection
+
+ returns 0 if ok, negative otherwise
+ """
if self.closed(): return -1
if self.connected(): return -1
self.addr = addr
self.conn = conn
- self.status = "connected"
+ self.status = self.STATUS_CONNECTED
self.handleOutput()
return 0
def disconnect(self):
+ """Disconnect the TCP connection to the console.
+ """
if self.conn:
self.conn.loseConnection()
self.addr = None
self.listen()
def requestReceived(self, msg, type, subtype):
+ """Receive console data from the console channel.
+
+ msg console message
+ type major message type
+ subtype minor message typ
+ """
self.rbuf.write(msg.get_payload())
self.handleOutput()
def responseReceived(self, msg, type, subtype):
- # Just ignore responses.
+ """Handle a response to a request written to the console channel.
+ Just ignore it because the return values are not interesting.
+
+ msg console message
+ type major message type
+ subtype minor message typ
+ """
pass
def produceRequests(self):
- # Send as much pending console data as there is room for.
+ """Write pending console data to the console channel.
+ Writes as much to the channel as it can.
+ """
work = 0
while not self.wbuf.empty() and self.channel.writeReady():
msg = xu.message(CMSG_CONSOLE, 0, 0)
def handleInput(self, conn, data):
"""Handle some external input aimed at the console.
- Called from a TCP connection (conn).
+ Called from a TCP connection (conn). Ignores the input
+ if the calling connection (conn) is not the one connected
+ to the console (self.conn).
+
+ conn connection
+ data input data
"""
if self.closed(): return -1
if conn != self.conn: return 0